define(['app'], function(app) {
	'use strict';

	var PAGE_LIMIT = 15;

	var controller = function controller($scope, $rootScope, Searchpro_manufactor, $window, $timeout, $ionicSlideBoxDelegate, $ionicSideMenuDelegate, GoPage, Auth, GoDialog) {
		$scope.isLoad = false; //可以加载

		// 未使用
		$scope.data = {
			param: {},

			inited: false, isend: false,

			cPage: 1, tPage: null,
			list: []
		}

		//画面加载
		$scope.$on('$ionicView.afterEnter', function() {

			//动态设置高度
			var all = $window.innerHeight;
			// 计算滚动条
			$scope.myObj = {
				"height": all - 44 + "px"
			};

			$scope.init_page(true);

		}, false);


		//下拉刷新 判断哪个模块进行刷新
		$scope.doRefresh = function() {
			$scope.init_page(true, 'refresh');
		};

		//加载更多
		$scope.doRefreshMore = function() {
			if ($scope.isLoad) return;
			$scope.isLoad = true;

			$scope.init_page(false, 'more');
		};

		// 初始化页面
		$scope.init_page = function (isInit, completeFlag) {

			var data = $scope.data

			if (isInit) data.isend = false;

			if (data.isend) return;

			var param = {
				status: data.status,
				pagelimit: PAGE_LIMIT,
				nPage: isInit ? 1 : (data.cPage - 0 + 1),
			}

			Searchpro_manufactor.search(param, function(res) {
				if (res.success != true) {
					GoDialog.toast(res.msg);
					return;
				}

				data.inited = true;

				var result = res.result;
				var list = result.list || [];

				data.cPage = result.page.cPage;
				data.tPage = result.page.tPage;

				if (data.cPage >= data.tPage) data.isend = true;

				if (param.nPage == 1) {
					data.list = list || [];
				} else {
					data.list = data.list || [];
					for (var i = 0; i < list.length; i++) {
						data.list.push(list[i]);
					} 
				}
				
			}, null, function() {
				if (completeFlag === 'refresh')	{
					$scope.$broadcast('scroll.refreshComplete');
				}
				if (completeFlag === 'more') {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 2000);
				}
			})
		}

		$scope.select_menu = function(index) {
			var row = $scope.data.list[index];
			$rootScope.$broadcast('warehouseSelectRelativeManu', {
				id: row.store_id,
				name: row.store_name
			})

			GoPage.go_back();
		}
	}

	controller.$inject = ['$scope', '$rootScope', 'Searchpro_manufactor', '$window', '$timeout', '$ionicSlideBoxDelegate', '$ionicSideMenuDelegate','GoPage', 'Auth', 'GoDialog'];
	
	return app.registerController("searchpro_manufactorctrl",controller);
});